//
//  LoginOptionsMenuView.swift
//  ezyform
//
//  Created by Moennig Q on 21/7/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class LoginOptionsMenuView: UIView {
    var selectTypeClosure: ((LoginMethodOperation)->Void)?
    
    let backView = UIView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpView() {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        //cancel btn
        let bottomMargin: CGFloat = -(UIDevice.getBottomAreaHeight() + 10)
        let cancelBtn = BaseButton()
        cancelBtn.setTitle(R.string.localizable.cancel(), for: UIControl.State.normal)
        cancelBtn.setTitleColor(UIColor(hexStr: "#292D32"), for: UIControl.State.normal)
        cancelBtn.setFont(R.font.interMedium(size: 17))
        cancelBtn.layer.cornerRadius = 8
        cancelBtn.clipsToBounds = true
        cancelBtn.backgroundColor = .white
        self.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(bottomMargin)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(60)
        }
        //
        backView.backgroundColor = .white
        backView.layer.cornerRadius = Constant.BackViewCornerRadius
        backView.clipsToBounds = true
        self.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.bottom.equalTo(cancelBtn.snp.top).offset(-16)
            make.left.right.equalTo(cancelBtn)
            make.height.greaterThanOrEqualTo(0)
        }
        //按钮时间
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: UIControl.Event.touchUpInside)
    }
    func initMenuUI(){
        let list: [LoginMethodOperation] = LoginMethodServcie().getOtherLoginMethod(APPConfig.loginInfo.currentLoginMethod)
        //1.先删除所有的子视图
        for subView in backView.subviews{
            subView.removeFromSuperview()
        }
        //2.再初始化按钮
        let count = list.count
        //
        if count == 1 {//至少有一个
            let typeButton = self.generateTypeButton(list[0])
            backView.addSubview(typeButton)
            typeButton.snp.makeConstraints { make in
                make.top.equalTo(30)
                make.bottom.equalTo(-30)
                make.height.greaterThanOrEqualTo(0)
                make.centerX.equalToSuperview()
            }
            typeButton.addTarget(self, action: #selector(typeButtonClick(_:)), for: UIControl.Event.touchUpInside)
        }else if count == 2{//最多有两个
            //第一个btn
            let typeButton1 = self.generateTypeButton(list[0])
            backView.addSubview(typeButton1)
            typeButton1.snp.makeConstraints { make in
                make.top.equalTo(30)
                make.bottom.equalTo(-30)
                make.height.greaterThanOrEqualTo(0)
                make.centerX.equalToSuperview().multipliedBy(0.5)
            }
            //第二个btn
            let typeButton2 = self.generateTypeButton(list[1])
            backView.addSubview(typeButton2)
            typeButton2.snp.makeConstraints { make in
                make.top.equalTo(30)
                make.bottom.equalTo(-30)
                make.height.equalTo(typeButton1)
                make.centerX.equalToSuperview().multipliedBy(1.5)
            }
            
            typeButton1.addTarget(self, action: #selector(typeButtonClick(_:)), for: UIControl.Event.touchUpInside)
            
            typeButton2.addTarget(self, action: #selector(typeButtonClick(_:)), for: UIControl.Event.touchUpInside)
        }
    }
    @objc func typeButtonClick(_ sender: LayoutButton){
        if let title = sender.titleLabel?.text {
            let resultStr = title.replacingOccurrences(of: " ", with: "").lowercased()//去掉touch id,或face id的空格
            if let operType = LoginMethodOperation(rawValue: resultStr){
                self.selectTypeClosure?(operType)
                self.removeThisView()
            }
        }
    }
    //
    @objc func cancelBtnClick(){
        self.removeThisView()
    }
    private func removeThisView(){
        if self.superview != nil{
            self.removeFromSuperview()
        }
    }
    private func generateTypeButton(_ loginType: LoginMethodOperation)->LayoutButton{
        let imageName: String = "login_options_menu_" + loginType.rawValue
        var description: String = loginType.rawValue.capitalized//gesture这么处理
        let typeButton = LayoutButton(.top, at: 16)
        if loginType == .faceID || loginType == .touchID{
            description = LoginMethodServcie().getBiometryTypeName(loginType)
        }
        typeButton.setTitle(description, for: UIControl.State.normal)
        typeButton.setImage(UIImage(named: imageName), for: UIControl.State.normal)
        typeButton.titleLabel?.font = R.font.interMedium(size: 15)
        typeButton.setTitleColor(UIColor(hexStr: "#292D32"), for: UIControl.State.normal)
        return typeButton
    }
}
